package com.whj.service;

import com.whj.common.serializer.JSONSerializer;
import com.whj.common.serializer.RpcDecoder;
import com.whj.common.serializer.RpcEncoder;
import com.whj.handler.Whj;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyService {
    public static void initService() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>(){
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new RpcEncoder(Object.class, new JSONSerializer()));
                        pipeline.addLast(new RpcDecoder(Object.class, new JSONSerializer()));
                        pipeline.addLast(new Whj());
                    }
                });
        ChannelFuture f = serverBootstrap.bind(9527).sync();
        System.out.println("tcp server start success..");
        f.channel().closeFuture().sync();
    }
}
